<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;

//use controller\BasicAdmin;
//use service\DataService;
//use service\ToolsService;
//use think\Db;
//use think\exception\HttpResponseException;
//use app\data\service\GoodsService;
//use think\admin\extend\CodeExtend;

use think\admin\Controller;
use think\facade\Request;
use think\facade\Db;

/**
 *  门户菜单
 * Class Meal
 * @package app\meal\controller
 */
class GatewayMenu extends Controller
{
    private $table = 'icon_details';

    /**
     * 门户菜单管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */

    public function index()
    {
        $this->title = '菜单图片';
        $this->_query($this->table)->where('is_deleted',0)->order('id desc')->page();
    }
    /**
     * 门户菜单添加
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post=$this->request->post();

            $data['iconImg']=$post['data'][0]['img'];
            $data['iconUrl']=$post['data'][0]['imgurl'];
            $data['title']=$post['data'][0]['menuname'];
            Db::name($this->table)->insert($data);
        }
        return $this->_form($this->table, 'form');
    }
    /**
     * 门户编辑
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $id = $this->request->get('id');
            $post=$this->request->post();
            $data['iconImg']=$post['data'][0]['img'];
            $data['iconUrl']=$post['data'][0]['imgurl'];
            $data['title']=$post['data'][0]['menuname'];
            Db::name($this->table)->where('id',$id)->update($data);
        }
        return $this->_form($this->table, 'form');;
    }

    /**
     * 修改状态
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function state()
    {
        $this->_save($this->table);
    }

    /**
     * 删除
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function del()
    {
        $id = $this->request->get('id');
        Db::name($this->table)->where('id',$id)->update(['is_deleted'=>1]);
        return $this->_form($this->table, 'index');
    }

    /**
     * 员工选择器
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function select()
    {
        if ($this->request->isPost())
        {

            $post = $this->request->post();
            $result = Db::name('icon_details')->where('id',$_GET['id'])->update(['onebtype'=>$post['select'],'onebmeal'=>$post['select']]);
            $this->success('修改成功!');
        }else{
            $result = Db::connect('zs')->table('meal')->where('status','<','4')->whereNotNull('lastname')->where('is_deleted',0)->column('lastname as name,id as value');
            $icon = Db::name('icon_details')->where('status',1)->where('is_deleted',0)->where('id',$_GET['id'])->value('onebtype');
            $icons = explode(",",$icon);
            foreach ($result as $key => $vo)
            {
                if (in_array($vo['value'],$icons))
                {
                    $result[$key]['selected']=true;
                }
            }

            $seled = Db::connect('zs')->table('meal')->where('status','<',4)->whereNotNull('lastname')->where('is_deleted',0)->whereIn('id',$icons)->column('lastname as name,id as value');
            $this->assign('seled',$seled);
            $this->assign('result',$result);
            return $this->fetch();
        }


    }


}
